//
//  HMCategoryViewController.m
//  HMMeituanHD
//
//  Created by apple on 2017/5/17.
//  Copyright © 2017年 itheima. All rights reserved.
//

#import "HMCategoryViewController.h"
#import "HMCategoryModel.h"
#import "HMCategoryTool.h"

static NSString *leftCell = @"leftCell";
static NSString *rightCell = @"rightCell";


@interface HMCategoryViewController ()<UITableViewDataSource, UITableViewDelegate>
//分类数据
@property (nonatomic, strong) NSArray <HMCategoryModel *> *categories;
//选中的一级菜单索引
@property (nonatomic, assign) NSInteger selecetedIndex;

@end

@implementation HMCategoryViewController

//创建成员变量
@synthesize popoverView = _popoverView;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}

#pragma mark - 懒加载

- (HMPopoverView *)popoverView{
    if (_popoverView == nil) {
        _popoverView = [HMPopoverView popoverView];
        
        [self.view addSubview:_popoverView];
        //设置二级联动视图的数据源
        _popoverView.leftTableView.dataSource = self;
        _popoverView.leftTableView.delegate = self;
        _popoverView.rightTableView.dataSource = self;
        _popoverView.rightTableView.delegate = self;
        //从工具类中获取分类数据
        self.categories = [HMCategoryTool getCategories];
    }
    return _popoverView;
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    //区分一级/二级菜单
    if (tableView == _popoverView.leftTableView) { //一级菜单
        
        return self.categories.count;
        
    } else { //二级菜单
    
        return self.categories[self.selecetedIndex].subcategories.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell;
    if (tableView == _popoverView.leftTableView) { //一级菜单
        
        cell = [tableView dequeueReusableCellWithIdentifier:leftCell];
        if (cell == nil) {
            
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:leftCell];
            //设置cell的背景图片
            cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_dropdown_leftpart"]];
            cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_dropdown_left_selected"]];
            
        }
        //设置数据
        cell.textLabel.text = self.categories[indexPath.row].name;
        //设置一级菜单图标
        cell.imageView.image = [UIImage imageNamed:self.categories[indexPath.row].small_icon];
        cell.imageView.highlightedImage = [UIImage imageNamed:self.categories[indexPath.row].small_highlighted_icon];
        //设置详情箭头
        if (self.categories[indexPath.row].subcategories.count) {
            
            //设置箭头
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
        } else { //避免重用,箭头错误设置
        
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        
        
    } else { //二级菜单
    
        cell = [tableView dequeueReusableCellWithIdentifier:rightCell];
        if (cell == nil) {
            
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:rightCell];
            //设置cell的背景图片
            cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_dropdown_rightpart"]];
            cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_dropdown_right_selected"]];
            
        }
        //设置数据
        cell.textLabel.text = self.categories[self.selecetedIndex].subcategories[indexPath.row];
    }
    
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (tableView == _popoverView.leftTableView) { //选择一级菜单
        
        if (self.categories[indexPath.row].subcategories) {
            
            //记录一级索引,用于设置二级数据源
            self.selecetedIndex = indexPath.row;
            //刷新二级数据
            [_popoverView.rightTableView reloadData];
            
        } else { //发送一级通知
        
            [HMNoteCenter postNotificationName:HMCategoryDidChangeNote object:nil userInfo:@{HMCategoryDidChangeNoteModelKey: self.categories[indexPath.row]}];
            
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        
    }else { //选择二级菜单
        
        [HMNoteCenter postNotificationName:HMCategoryDidChangeNote object:nil userInfo:@{HMCategoryDidChangeNoteModelKey: self.categories[self.selecetedIndex], HMCategoryDidChangeNoteSubtitleKey: self.categories[self.selecetedIndex].subcategories[indexPath.row]}];
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}


@end
